/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.util;

import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class ByteArrayUtil {
    public static int firstOccurence(byte[] array, char ch) {
        return firstOccurence(array, ch, 0, -1);
    }

    public static int firstOccurence(byte[] array, char ch, int start) {
        return firstOccurence(array, ch, start, -1);
    }

    public static int firstOccurence(byte[] array, char ch, int start, int end) {
        int result = -1;
        if (end == -1 || end > array.length) end = array.length;
        if (start >= 0)
            for (int i = start; i < end; i++) {
                if (array[i] == ch) {
                    result = i;
                    break;
                }
            }
        return result;
    }

    public static int lastOccurence(byte[] array, char ch) {
        return lastOccurence(array, ch, 0, -1);
    }

    public static int lastOccurence(byte[] array, char ch, int start) {
        return lastOccurence(array, ch, start, -1);
    }

    public static int lastOccurence(byte[] array, char ch, int start, int end) {
        int result = -1;
        if (end == -1 || end > array.length) end = array.length;
        if (start >= 0)
            for (int i = end - 1; i >= start; i--) {
                if (array[i] == ch) {
                    result = i;
                    break;
                }
            }
        return result;
    }

    public static int firstMissing(byte[] array, char ch) {
        return firstMissing(array, ch, 0, -1);
    }

    public static int firstMissing(byte[] array, char ch, int start) {
        return firstMissing(array, ch, start, -1);
    }

    public static int firstMissing(byte[] array, char ch, int start, int end) {
        int result = -1;
        if (end == -1 || end > array.length) end = array.length;
        if (start >= 0)
            for (int i = start; i < end; i++) {
                if (array[i] != ch) {
                    result = i;
                    break;
                }
            }
        return result;
    }

    public static int lastMissing(byte[] array, char missing, int start, int end) {
        int result = -1;
        if (end == -1 || end > array.length) end = array.length;
        if (start >= 0)
            for (int i = end - 1; i >= start; i--) {
                if (array[i] != missing) {
                    result = i;
                    break;
                }
            }
        return result;
    }

    public static int firstMissingBefore(byte[] array, char missing, char before, int start, int end) {
        int result = -1;
        if (end == -1 || end > array.length) end = array.length;
        if (start >= 0)
            for (int i = start; i < end; i++) {
                if (array[i] == before)
                    break;
                if (array[i] != missing)
                    result = i;
            }
        return result;
    }

    public static int firstEndOfLine(byte[] array, int start) {
        int result = -1;
        if (start >= 0)
            for (int i = start; i < array.length - 1; i++) {
                if (array[i] == 0x0D && array[i + 1] == 0x0A) {
                    result = i;
                    break;
                }
            }
        return result;
    }

    public static boolean equals(byte[] data1, int start1, int end1, byte[] data2, int start2, int end2) {
        boolean result = false;
        if (data1 == null && data2 == null) result = true;
        else if (data1 != null && data2 != null) {
            if (end1 - start1 != end2 - start2)
                result = false;
            else {
                int i = start1;
                int j = start2;
                result = true;
                while (i < end1)
                    if (data1[i++] != data2[j++]) {
                        result = false;
                        break;
                    }
            }
        }
        return result;
    }

    public static long parseInteger(byte[] data, int start, int end) throws ParseException {
        long result = 0;
        for (int i = start; i < end; i++) {
            final byte digit = data[i];
            if (digit < 48 || digit > 57)
                throw new ParseException("Cannot parse: it is not integer!", i);
            result = result * 10 + digit - 48;
        }
        return result;
    }

    public static boolean contains(byte[] data, int start, int end, byte[] sample, int sstart, int send) {
        int i = start;
        int j = sstart;
        boolean result = false;
        while (end - i >= send - j && j < send) {
            if (data[i++] == sample[j]) {
                j++;
                result = true;
            } else {
                j = sstart;
                result = false;
            }
        }
        return result;
    }

    public static boolean startsWith(byte[] data, int start, int end, byte[] sample, int sstart, int send) {
        int j = sstart;
        for (int i = start; i < end && j < send; i++) {
            if (data[i] != sample[j++])
                return false;
        }
        return true;
    }
}
/*
 * $Log$
 */